package com.example.cursor_orm.dao.impl;

import com.example.cursor_orm.dao.PermissionDAO;
import com.example.cursor_orm.model.Permission;
import com.example.cursor_orm.util.DatabaseUtil;
import org.springframework.stereotype.Repository;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Repository
public class PermissionDAOImpl implements PermissionDAO {
    
    @Override
    public int insert(Permission permission) {
        String sql = "INSERT INTO permission (name, description, parent_id) VALUES (?, ?, ?)";
        return DatabaseUtil.executeUpdate(sql, permission.getName(), permission.getDescription());
    }
    
    @Override
    public int deleteById(Integer id) {
        String sql = "DELETE FROM permission WHERE id = ?";
        return DatabaseUtil.executeUpdate(sql, id);
    }
    
    @Override
    public int update(Permission permission) {
        String sql = "UPDATE permission SET name = ?, description = ?, parent_id = ? WHERE id = ?";
        return DatabaseUtil.executeUpdate(sql, permission.getName(), permission.getDescription(), permission.getId());
    }
    

    @Override
public Permission findById(Integer id) {
    String sql = "SELECT * FROM permission WHERE id = ?";
    try (ResultSet rs = DatabaseUtil.executeQuery(sql, id)) {
        return mapResultSetToPermission(rs);
    } catch (SQLException e) {
        e.printStackTrace();
        return null;
    }
}

    @Override
public List<Permission> findAll() {
    String sql = "SELECT * FROM permission ORDER BY id";
    try (ResultSet rs = DatabaseUtil.executeQuery(sql)) {
        return mapResultSetToPermissionList(rs);
    } catch (SQLException e) {
        e.printStackTrace();
        return new ArrayList<>();
    }
}

    
    @Override
public List<Permission> findByCondition(String condition) {
    String sql = "SELECT * FROM permission WHERE " + condition + " ORDER BY id";
    try (ResultSet rs = DatabaseUtil.executeQuery(sql)) {
        return mapResultSetToPermissionList(rs);
    } catch (SQLException e) {
        e.printStackTrace();
        return new ArrayList<>();
    }
}

    
    @Override
    public int count() {
        String sql = "SELECT COUNT(*) FROM permission";
        try (ResultSet rs = DatabaseUtil.executeQuery(sql)) {
            if (rs != null && rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    
    @Override
public Permission findByName(String name) {
    String sql = "SELECT * FROM permission WHERE name = ?";
    try (ResultSet rs = DatabaseUtil.executeQuery(sql, name)) {
        return mapResultSetToPermission(rs);
    } catch (SQLException e) {
        e.printStackTrace();
        return null;
    }
}





    
    @Override
public List<Permission> findByUserId(Integer userId) {
    String sql = "SELECT DISTINCT p.* FROM permission p " +
                 "JOIN role_permission rp ON p.id = rp.permission_id " +
                 "JOIN user_role ur ON rp.role_id = ur.role_id " +
                 "WHERE ur.user_id = ? ORDER BY p.id";
    try (ResultSet rs = DatabaseUtil.executeQuery(sql, userId)) {
        return mapResultSetToPermissionList(rs);
    } catch (SQLException e) {
        e.printStackTrace();
        return new ArrayList<>();
    }
}

    
    /**
     * 将ResultSet映射为Permission对象
     */
    private Permission mapResultSetToPermission(ResultSet rs) {
        try {
            if (rs != null && rs.next()) {
                Permission permission = new Permission();
                permission.setId(rs.getInt("id"));
                permission.setName(rs.getString("name"));
                permission.setDescription(rs.getString("description"));
                return permission;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 将ResultSet映射为Permission对象列表
     */
    private List<Permission> mapResultSetToPermissionList(ResultSet rs) {
        List<Permission> permissions = new ArrayList<>();
        try {
            if (rs != null) {
                while (rs.next()) {
                    Permission permission = new Permission();
                    permission.setId(rs.getInt("id"));
                    permission.setName(rs.getString("name"));
                    permission.setDescription(rs.getString("description"));
                    permissions.add(permission);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return permissions;
    }
} 